const app = getApp();
const utils = require('../../utils/util.js');
Component({
  /**
   * 自定义底部导航组件（平台首页）
   * 组件的属性列表
   */
  properties: {
    currPagePath: {
      type: String,
      value: '/pages/index/index'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    color: '#8a8a8a',
    selectedColor: '#FB8524',
    isAccredit: true,
    TabbarList: [{
      id: 0,
      pagePath: '/pages/indexDefault/index',
      text: '玖玖客',
      iconPath: '/images/tabbar/logooff.png',
      selectedIconPath: '/images/tabbar/logoon.png',
      isActive: true,
      target: 'relaunch'
    },
    {
      id: 2,
      pagePath: '/fenxiao/home/index',
      text: '服务商',
      iconPath: '/images/tabbar/3.png',
      selectedIconPath: '/images/tabbar/4.png',
      isActive: false,
      target: 'relaunch'
    },
    {
      id: 1,
      pagePath: '/merchant/merchants/myCardCoupon/index',
      text: '我的',
      iconPath: '/images/tabbar/29.png',
      selectedIconPath: '/images/tabbar/30.png',
      isActive: false,
      target: 'relaunch'
    }]
  },
  ready: function () {
    this._initTabBarPages();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    _initTabBarPages: function () {
      var that = this;
      var newPages = that.data.TabbarList;
      // var newTabbar = [];
      newPages.forEach((item) => {
        item.isActive = item.pagePath == that.data.currPagePath;
      })
      that.setData({
        TabbarList: newPages
      })
      if (app.globalData.userInfo && app.globalData.userInfo.Avatar.indexOf('userdefault.png') != -1) {
        that.setData({
          isAccredit: false
        })
      }

      // detail对象，提供给事件监听函数
      // this.triggerEvent('tabbarevent', {
      //   newTabbar: newTabbar
      // })
    },

    getUserInfo: function (e) {
      if (e.detail.iv) {
        if (!isAccredit) {//弹出授权框且同意
          // wx.login({
          //   success: function (res) {
          //     utils.AuthorizeUserInfo(e, res.code, function () {});
          //   }
          // })
          let loginCode = '';
          wx.login({
            success: function (res) {
              loginCode = res.code
            }
          })
          utils.AuthorizeUserInfo(res, loginCode, function (flag) {
            if (flag) {

            }
          });
        }
      }
    },
    // 单击tab触发的函数
    onTabbarItemTap(ev) {
      let key = 0;
      if (ev.detail.target) {
        key = ev.detail.target.dataset.key;
      } else {
        key = ev.currentTarget.dataset.key;
      }
      var link = this.data.TabbarList[key];
      var target = link.target.toLowerCase();
      var targetUrl = link.pagePath;
      utils.subscribe();
      if (target == 'relaunch') {
        wx.reLaunch({
          url: targetUrl + "?isPlatform=1"
        })
      } else if (target == 'navigateto') {
        wx.navigateTo({
          url: targetUrl
        })
      } else if (target == 'redirectto') {
        wx.redirectTo({
          url: targetUrl
        })
      }
    }
  }
})